package daobeauty.interceptor.invoke.impl;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;

import daobeauty.interceptor.invoke.AbstractCurrentSessionInvokeStrategy;
import daobeauty.util.MethodSignatureUtils;

public class CreateCriteriaInvoke extends AbstractCurrentSessionInvokeStrategy {
	private static final Log LOG = LogFactory.getLog(CreateCriteriaInvoke.class);
	
	public CreateCriteriaInvoke(Method method, SessionFactory sessionFactory, Class domainClass) {
		super(sessionFactory, domainClass);
		final Class [] parameterTypes = method.getParameterTypes();
		if (parameterTypes!=null && parameterTypes.length>0){
			throw new IllegalArgumentException("method "+MethodSignatureUtils.toMethodSignature(method)+" must have no parameters.");
		}
		final Class returnType = method.getReturnType();
		if (returnType==null || !Criteria.class.isAssignableFrom(returnType)){
			throw new IllegalArgumentException("method "+MethodSignatureUtils.toMethodSignature(method)+" must have return type compatible with "+Criteria.class.getName());
		}
		if (LOG.isInfoEnabled()){
			LOG.info("created createCriteria strategy for method "+MethodSignatureUtils.toMethodSignature(method));
		}
	}

	public Object invoke(String methodName, Object[] args) throws Exception {
		return createCriteria();
	}
}
